﻿namespace EFS.Common.UI.Controls.NotificationDialog
{
	public static class ProgressDialog
	{
		#region Composition

		/// sync
		private static object syncAccess = new object();

		/// modal dialog
		private static EFS.Common.UI.Controls.NotificationDialog.ProgressModal modalDialog = null;

		#endregion

		#region Usage

		/// <summary>
		/// Show 
		/// </summary>
		/// <param name="key"></param>
		/// <param name="message"></param>
		public static void InProgress(System.Object key, System.String message, params object[] args)
		{
			// add to window ui scheduler
			System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
			{
				lock (syncAccess)
				{
					// check
					if (null == modalDialog)
					{
						// new
						modalDialog = new EFS.Common.UI.Controls.NotificationDialog.ProgressModal();
						modalDialog.Closed += new System.EventHandler(modalDialog_Closed);

						// show
						modalDialog.Show();

						// append message
						modalDialog.Append(key, System.String.Format(message, args));
					}
					else
					{
						// append message
						modalDialog.Append(key, System.String.Format(message, args));
					}
				}
			});
		}

		/// Close
		public static void CancelAll()
		{
			// add to window ui scheduler
			System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
			{
				lock (syncAccess)
				{
					// already closed
					if (null == modalDialog)
					{
						return;
					}

					// calcel all
					modalDialog.CancelAll();
				}
			});
		}

		/// Close
		public static void Completed(System.Object key)
		{
			// add to window ui scheduler
			System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
			{
				lock (syncAccess)
				{
					// already closed
					if (null == modalDialog)
					{
						return;
					}

					// complete
					modalDialog.Completed(key);
				}
			});
		}

		/// <summary>
		/// Close
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private static void modalDialog_Closed(object sender, System.EventArgs e)
		{
			lock (syncAccess)
			{
				// clean
				modalDialog = null;
			}
		}

		#endregion
	}
}